package com.zangchat.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.zangchat.mapper.ChatSysConfigsMapper;
import com.zangchat.domain.ChatSysConfigs;
import com.zangchat.service.IChatSysConfigsService;

import java.math.BigDecimal;
import java.util.List;

/**
 * 商城总配置 服务层实现
 *
 * @author ruoyi
 * @date 2020-07-29
 */
@Service
public class ChatSysConfigsServiceImpl extends ServiceImpl<ChatSysConfigsMapper, ChatSysConfigs> implements IChatSysConfigsService
{
    @Override
    public  <T> T getValue(String fieldCode, Class<T> clazz) {
        if(fieldCode == null || fieldCode.length() <= 0 || clazz == null) {
            return null;
        }
        ChatSysConfigs chatSysConfigs=new ChatSysConfigs();
        chatSysConfigs.setFieldCode(fieldCode);
        QueryWrapper<ChatSysConfigs> queryWrapper=new QueryWrapper<>(chatSysConfigs);
        ChatSysConfigs sysConfigs = baseMapper.selectOne(queryWrapper);

        if (sysConfigs==null){
            return null;
        }

        String fieldValue = sysConfigs.getFieldValue();
        if(clazz == int.class || clazz == Integer.class) {
            return (T)Integer.valueOf(fieldValue);
        }else if(clazz == String.class) {
            return (T)fieldValue;
        }else if(clazz == long.class || clazz == Long.class) {
            return  (T)Long.valueOf(fieldValue);
        }else if(clazz == BigDecimal.class ) {
            return   (T)new BigDecimal(fieldValue);
        }else {
            return JSON.toJavaObject(JSON.parseObject(fieldValue), clazz);
        }
    }
}